//
//  SHHDataHelper.swift
//  DoctorVisits
//
//  Created by lanou3g on 16/2/29.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

//用来传送RotateModel对象
typealias Block = (_ :Array<RotateModel>)->Void
//用来传送recname字符串
typealias Block1 = (_ :Array<String>)->Void
//用来传送TitleModel对象
typealias myBlock = (_ :Array<TitleModel>)->Void

class SHHDataHelper: NSObject {
    
    //申明数组，存放解析出来的model
    //存放的是轮播图的model
    var recommendArray = Array<RotateModel>()
    //申明数组，存放解析出来的分区头的标题
    var recnameArray = Array<String>()
    
    //申明数组，存放解析出来的title和电影的信息
    var titleAndMovieArray = Array<TitleModel>()
    
 
    
    //创建单例对象
    //为了防止在类的其他地方调用该初始化方法创建对象
    //保证了唯一性
    private override init() {}
    static let shareInstance = SHHDataHelper()
    
    func requestAllModelWithUrlString(urlString:String,success:Block)->Void {
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            //                print(data)
            
            do{
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let dict:NSDictionary = dic["data"] as!NSDictionary
                let array:NSArray = dict["focusdata"] as!NSArray
                for rotateDic in array {
                    let rotate = RotateModel()
                    rotate.setValuesForKeysWithDictionary(rotateDic as! [String : AnyObject])
//                    print(rotate.name)
                    self.recommendArray.append(rotate)
                }
                
            }catch {
                
            }
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.recommendArray)
            })
            
        })
        task.resume()
        
    }
    
    
    //解析recname的url
    func requestAllRecnameWithUrlString(urlString:String,success:myBlock)->Void {
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            //                print(data)
            
            do{
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let dict:NSDictionary = dic["data"] as!NSDictionary
                let array:NSArray = dict["recdatas"] as!NSArray
                for recname in array {
                    let title = TitleModel()
                    //申明数组,存放解析出来的影片
                    var movieArray = Array<RotateMovie>()
                    
                    for movieDic in (recname["data"] as!NSArray) {
                        let movie = RotateMovie()
                        movie.setValuesForKeysWithDictionary(movieDic as! [String : AnyObject])
                        movieArray.append(movie)
                    }
                    title.movie = movieArray
                    title.setValuesForKeysWithDictionary(recname as! [String : AnyObject])
                    self.titleAndMovieArray.append(title)
                }
                
            }catch {
                
            }
//            print("title\(self.titleAndMovieArray[0].movie![0].name!)")
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                success(self.titleAndMovieArray)
            })
            
        })
        task.resume()
        
    }
    
    //返回recname对应的字符串
    func backrecnameString(index:Int)->String {
//        let string = "|"+self.recnameArray[index]
        let string = "|"+self.titleAndMovieArray[index].recname!
        return string
    }
    
}
